<?php
class EventAttendee extends EventsAppModel {

	var $name = 'EventAttendee';

	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $belongsTo = array(
			'Event' => array(
					'className' => 'Events.Event',
					'foreignKey' => 'event_id',
					'counterCache' => 'whosin_count'
	),
			'User' => array(
					'className' => 'Users.User',
					'foreignKey' => 'user_id',
					'counterCache' => 'whosin_count'
	)
	);
	
	function getAttendees($eventId, $includeEventModelData = false)
	{
		if($includeEventModelData === false)
		{
			$this->unbindModel(array("belongsTo" => array("className" => "Event")), true);
		}
		return $this->find("all",array("conditions"=>array($this->alias.".event_id"=>$eventId),"recursive"=>0));
	}
	
	function isAttendee($eventId, $userId)
	{
		return $this->hasAny(array($this->alias.".event_id" => $eventId, $this->alias.".user_id"=>$userId));
	}
	
	function addAttendee($eventId, $userId, $description = false)
	{
		$result = false;
		if($this->Event->hasAny(array("Event.id"=>$eventId)) && $userId)
		{
			if($description == false)
			{
				$description = __("Added automatically");
			}
			$this->create();
			$result = $this->save(array($this->alias => array("event_id" => $eventId, "user_id" => $userId, "description"=>$description)));
		}
		return $result;
	}
	
	function revokeAttendee($eventId, $userId)
	{
		$result = false;
		if($this->hasAny(array($this->alias.".event_id"=>$eventId, $this->alias.".user_id"=>$userId)))
		{
			//$result = $this->deleteAll(array($this->alias.".event_id"=>$eventId, $this->alias.".user_id"=>$userId));
			$id = $this->field("id", array($this->alias.".event_id"=>$eventId, $this->alias.".user_id"=>$userId));
			if($id)
			{
				$result = $this->del($id);
			}
		}
		return $result;
	}

}
?>